package leetcode.code0522;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int findLUSlength(String[] strs) {
		Arrays.sort(strs, (a, b) -> b.length() - a.length());
		int len = strs.length;
		for (int i = 0; i < len; i++) {
			String cur = strs[i];
			int p = 0;
			boolean ok = true;
			while (p < len && strs[p].length() >= cur.length()) {
				if (p != i) {
					if (this.check(cur, strs[p])) {
						ok = false;
					}
				}
				p++;
			}
			if (ok) {
				return cur.length();
			}
		}
		return -1;
	}

	private boolean check(String cur, String string) {
		for (int i = 0, p = 0; i < cur.length(); i++) {
			char c = cur.charAt(i);
			while (p < string.length() && string.charAt(p) != c) {
				p++;
			}
			if (p >= string.length()) {
				return false;
			}
			p++;
		}
		return true;
	}

	@Override
	public void debug4() {
		H.compare(-1, this.findLUSlength(
				H.str2array2("[\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"]")));
		H.compare(7, this.findLUSlength(H.str2array2("[\"aabbcc\", \"aabbcc\",\"bc\",\"bcc\",\"aabbccc\"]")));
		H.compare(2, this.findLUSlength(H.str2array2("[\"aabbcc\",\"aabbcc\",\"cb\"]")));
		H.compare(3, this.findLUSlength(H.str2array2("[\"aba\",\"cdc\",\"eae\"]")));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
